import { _decorator, Component, Node, Vec2, Label, Sprite, UIOpacity, SpriteFrame, Vec3, sys, Color } from 'cc';
import { GameSticker, GameStickerType } from '../../Model/GameStickerMgr';
import { Unit } from '../Unit';
import { UICtrl } from '../../Ctrl/UICtrl';
import AssetsMgr from '../../Model/AssetsMgr';
import { Global, TechnologyType, UnitMsg, UnitType } from '../../Util/Global';
import GameEventMgr, { GameEvent } from '../../Model/GameEventMgr';
import PoolManager from '../../Ctrl/PoolManager';
import { EconomicCom } from '../Component/EconomicCom';
import { Tools } from '../../Util/Tools';
const { ccclass, property } = _decorator;

@ccclass('Tracker')
export class Tracker extends GameSticker {

    type: GameStickerType = GameStickerType.Tracker;

    setTarget(target: any) {
        this.target = target;

        UICtrl.Instance.uiGraphicsTran.node.getWorldPosition(this.toward);
        this.selfOpacity.opacity = 0;


        if (target.getComponent("FactoryCom")) {
            AssetsMgr.getRes("texture/death-star/spriteFrame", SpriteFrame, (icon) => {
                this.icon.spriteFrame = icon;
            });
        }
        else {
            AssetsMgr.getRes("texture/moon-orbit/spriteFrame", SpriteFrame, (icon) => {
                this.icon.spriteFrame = icon;
            });
        }
    }
    // toward: Unit = null;

    toward: Vec3 = new Vec3();

    @property(UIOpacity)
    selfOpacity: UIOpacity = null;
    @property(Vec2)
    forward: Vec2 = new Vec2();
    @property(Node)
    textNode: Node = null;
    @property(Label)
    text_distance: Label = null;
    @property(Label)
    text_mssage: Label = null;
    @property(UIOpacity)
    txtOpacity: UIOpacity = null;
    @property(Sprite)
    icon: Sprite = null;

    distance: number = 0;
    start() {
        // this.toward = Global.gameManager.playerCtrl.playerUnit;
        if (this.forward.length() <= 0) {
            this.forward.x = 0;
            this.forward.y = 1;
        }

        // this.icon.color = this.target.GetBodyColor();
        this.textNode.active = Global.gameManager.playerData.technologyProduct.has(TechnologyType.PlanetWarning);
        GameEventMgr.Register(this, GameEvent.ActivateTechnology, this.onActivateTechnologyEventHandle.bind(this));

        this.text_distance.string = '';
        this.text_mssage.string = '';

        if (Global.TrackerDebug) {
            this.textNode.active = true;
            // this.icon.node.active = false;
            // this.text.node.setPosition(0, 0);
            this.text_distance.string = this.target.uuid.slice(0, 11);
        }
        // this.text_distance.fontSize = 10;
    }

    protected onEnable(): void {
        // this.toward = Global.gameManager.playerCtrl.playerUnit;
    }

    protected onDisable(): void {
        this.icon.color = Color.WHITE;
        this.text_distance.color = Color.WHITE;
        this.text_mssage.color = Color.WHITE;
    }

    onActivateTechnologyEventHandle(type: TechnologyType) {
        if (type == TechnologyType.PlanetWarning) {
            this.textNode.active = true;
        }
    }

    update(deltaTime: number) {
        if (this.selfOpacity.opacity != 0) {
            this.UpdateMessage();
            this.node.setScale(Global.CameraOrthoScale, Global.CameraOrthoScale);
        }
        if (!Global.TrackerDebug) {
            this.txtOpacity.opacity -= deltaTime * 100;
        }
        else {
            this.textNode.active = true;
            this.txtOpacity.opacity = 255;
        }
    }

    private self_world_pos: Vec2 = new Vec2;
    UpdateMessage() {
        // if (!this.toward) {
        //     this.toward = Global.gameManager.playerCtrl.playerUnit;
        // }
        // if (!this.toward || this.toward.death) {

        //     PoolManager.putNode('Tracker', this.node);
        //     // this.node.destroy();
        // }
        if (this.target) {
            // let toward = this.toward.unitWorldPos_v2.subtract(this.target.unitWorldPos_v2);
            this.self_world_pos.set(this.node.worldPosition.x, this.node.worldPosition.y);
            let toward = this.target.unitWorldPos_v2.subtract(this.self_world_pos);
            this.distance = Math.floor(toward.length());

            let angle = this.forward.signAngle(toward) * 180 / Math.PI;
            this.node.setRotationFromEuler(0, 0, angle); //朝向目标

            this.textNode.setRotationFromEuler(0, 0, -angle);//设置文字保持水平
        }
        else {

            PoolManager.putNode('Tracker', this.node);
        }
    }

    //label太耗性能了不能每帧刷新
    UpdateLabel() {
        if (!Global.TrackerDebug && this.target) {
            let color = Color.WHITE;
            let name = UnitMsg.msgMap.get(this.target.unitType).name;
            if (this.target.unitType == UnitType.UnitPlanet) {
                if (this.target.getComponent(EconomicCom)) {
                    name = '生命星球';
                    color = Color.RED;
                }
            }
            else if (this.target.unitType == UnitType.UnitSun) {
                color = Tools.hex2color('#DD7979');
            }
            else if (this.target.unitType == UnitType.UnitAsteroid) {
                color = Tools.hex2color('#EEFF72');
            }

            this.text_distance.string = this.distance.toString();
            this.text_distance.color = color;
            this.text_mssage.string = name;
            this.text_mssage.color = color;
            this.icon.color = color;

            this.txtOpacity.opacity = 255;
        }
    }
}